import {
  Effect,
  Reducer,
} from 'umi';

import {
  getAllRecommendItem,
  getArticleCategoryList,
  getArticleClass,
  getArticleDetail,
  getArticleList,
  getArticleTag,
  getArticleTagAll,
  getRecommendedReading,
} from '@/services';
import { IArticleItem } from '@/types';

import {
  allRecommendInit,
  ArticleTagType,
} from '../types/modules/article';

export interface ArticleModelState {
  articleList: IArticleItem[]
  articleListPage2: IArticleItem[],
  articleLength: number,
  articleCategoryList: IArticleItem[],
  allRecommendItem: allRecommendInit[],
  RecommendedReading: IArticleItem[],
  ArticleTag: ArticleTagType[],
  ArticleTagPage: IArticleItem[],
  articleClass: ArticleTagType[],
  articleDetail:IArticleItem
}

export interface ArticleModelType {
  namespace: 'article';
  state: ArticleModelState;
  effects: {
    getArticleList: Effect;
    getArticleCategoryList: Effect,
    getArticleListPage2: Effect;
    allRecommend: Effect;
    getRecommendedReading: Effect;
    getArticleTag: Effect;
    getArticleTagAll: Effect;
    getArticleClass: Effect;
    getArticleDetail: Effect;
  };
  reducers: {
    save: Reducer<ArticleModelState>;
  };
}

const ArticleModel: ArticleModelType = {
  //等价于vuex的namespace   相当于redux的子reduce
  namespace: 'article',
  //定义的状态
  state: {
    articleList: [],
    articleLength: 0,
    articleCategoryList: [],
    articleListPage2: [],
    allRecommendItem: [],
    RecommendedReading: [],
    ArticleTag: [],
    ArticleTagPage: [],
    articleClass: [],
    articleDetail:{} as IArticleItem
  },
  //副作用 对应的异步操作，常指网络请求
  effects: {
    // 获取文章详情信息
    *getArticleDetail({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleDetail, payload);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            articleDetail: result.data
          }
        })
      }
    },

    // 获取全部Tag数据
    *getArticleClass({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleClass, payload);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            articleClass: result.data
          }
        })
      }
    },
    *getArticleTagAll({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleTagAll, payload);
      console.log(result);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            ArticleTag: result.data
          }
        })
      }
    },
    // 获取分页tag数据
    *getArticleTag({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleTag, payload);
      console.log(result);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            ArticleTagPage: result.data
          }
        })
      }
    },
    *getRecommendedReading({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getRecommendedReading);
      console.log(result);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            RecommendedReading: result.data
          }
        })
      }
    },
    *getArticleCategoryList({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleCategoryList, payload);
      console.log(result);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            articleCategoryList: result.data
          }
        })
      }
    },
    *getArticleList({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleList, payload);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            articleList: result.data[0],
            articleLength: result.data[1]
          }
        })
      }
    },
    *getArticleListPage2({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getArticleList, payload);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            articleListPage2: result.data[0],
            articleLength: result.data[1]
          }
        })
      }
    },
    *allRecommend({ payload }, { call, put }) {
      //call改变this指向
      const result = yield call(getAllRecommendItem);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            allRecommendItem: result.data[0]
          }
        })
      }
    },
  },

  //同步操作
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default ArticleModel;
